define(backboneCommon.getViewDefine("media.index"), function ($,require,Collection,tpl) {
    var backboneView= Backbone.View.extend({
        el: "#container_body",
        events: {
            "keydown #search_keyword": "enterKeyword",
            "click #btn_search": "refreshList",
            'click #tb_listMedia .media-delete': 'deleteMedia'
        },
        initialize: function () {
            this.modelAttach={};
            this.modelAttach.baseData=this.model.get("modelBaseData");
            this.listenTo(this.modelAttach.baseData, 'bind:Os', this.bindOs);
            this.listenTo(this.model, 'bind:PageList', this.bindList);
            this.listenTo(this.model, 'save:Delete', this.deleteMediaCallback);
            this.render();
        },
        render:function(json){
            var templateHtml=_.template(tpl);
            this.$el.html(templateHtml(json));
        },
        initMain:function(){
            this.elementModel={};
            this.extObject={};
            this.pageData={};
            this.elementModel.table=$("#tb_listMedia");
            this.pageData.option=this.modelAttach.baseData.get("defSelectOption");
            this._initPageList();
        },
        bindOs:function () {
            var self=this;
            var res=this.modelAttach.baseData.get("dataListOs");
            $("#search_os").ExtSelect2({placeholder: "-操作系统-",infoExt:{data:res.data,defaultValue:self.pageData.option,
                changeCallback:function(){self.refreshList()}
            }});
            if(!res.success){ $.ExtNotify.error("操作系统数据获取失败："+res.msg);}
        },
        _initPageList: function () {
            var self=this;
            self.extObject.table=self.elementModel.table.ExtDataTable({
                columns: [
                    {"data": "id", sClass: "w-60","bSortable": false},
                    {"data": "name",sClass: "p-r-20","bSortable": false},
                    {"data": "osName",sClass: "w-130", "bSortable": false},
                    {"data": "appClassName", sClass: "w-200", "bSortable": false},
                    {"data": "spaces", sClass: "w-140","bSortable": false}
                ],
                infoExt:{
                    ajaxDataPara:function(){
                        var param= $.ExtAssist.initParamExt();
                        var keyword = $.trim($("#search_keyword").val());
                        if (!$.ExtCommon.isCheckEmpty(keyword)) {param.search = keyword;}
                        var os = $("#search_os").val();
                        if (!$.ExtCommon.isCheckEmpty(os,"-99")) {param.os = os;}
                        return param;
                    },
                    ajaxWholeCallback:function(param){
                        self.collection.getPageList(param)
                    }
                },
                createdRow: function (row, data) {
                    self._bindTableTd(row, data);
                },
                drawCallback: function () {}
            });
        },
        _bindTableTd:function(row,data){
            var elementTd=$('td', row);
            elementTd.eq(1).attr("id","media_"+data.id).html($("#template_mediaName").render({name: data.name, id: data.id,isdelete: parseInt(data.spaces)==0}));
            elementTd.eq(2).html(data.osName);
            elementTd.eq(3).html(data.appClassName);
            elementTd.eq(4).html(data.spaces+'个');
        },
        bindList:function(){
            var data=this.model.get("dataPageList");
            this.extObject.table.ajaxWholeRefreshData(data);
        },
        refreshList: function () {this.extObject.table.refresh()},
        enterKeyword:function(e){if(e.keyCode == 13) {this.refreshList()}},
        deleteMedia:function(e){
            var self=this;
            var tr=$(e.currentTarget).parents('tr:first');
            var trData =self.extObject.table.getRowInfo(tr).data();
            var id=trData.id;
            $.ExtSweetAlert.confirmSimple("您确定要删除该数据吗？",trData.name,function () {
                self.collection.operateDelete(id);
            });
        },
        deleteMediaCallback:function(){
            $.ExtNotify.destroy();
            var res = this.model.get('dataDelete');
            if (res.success) {
                $.ExtNotify.success("删除成功");
                this.refreshList();
            }else{
                $.ExtNotify.error(res.msg);
            }
            $.ExtSweetAlert.checkClose(res.success);
        }
    });
    var viewModel=function(){
        this.collection= new Collection();
        this.model=this.collection.model;
        this.view=new backboneView({collection:this.collection,model:this.model});
        this.view.initMain();
    };
    viewModel.prototype={};
    return viewModel;
});